package com.design.observer.service.impl;

import com.design.observer.service.DisplayElement;
import com.design.observer.service.NewWeatherData;

import java.util.Observable;
import java.util.Observer;

/**
 * @author zhengzihong
 * @version 1.0.0
 * @date 2018/9/8
 * @Description
 */
public class NewCurrentConditionDisplay implements DisplayElement,Observer {

    private float temperature;

    private float humidity;

    private Observable observable;

    /**
     * 注册成为观察者
     * @param observable
     */
    public NewCurrentConditionDisplay(Observable observable){
        this.observable = observable;
        observable.addObserver(this);
    }

    /**
     * 展示数据
     */
    @Override
    public void display() {
        System.out.println("Current conditions:"+ temperature+"F degrees and "+humidity+"% humidity");
    }


    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof  NewWeatherData){
            NewWeatherData weatherData = (NewWeatherData) o;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            display();
        }
    }
}
